﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Collections;

public partial class SendMail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
        /// 邮件发送方法(带附件)   
        /// </summary>   
        /// <param name="mailto">收件人地址。如：receiver@163.com</param>   
        /// <param name="mailsubject">邮件标题</param>   
        /// <param name="mailbody">邮件正文</param>   
        /// <param name="mailFrom">邮件发送人地址。如：sender@163.com</param>   
        /// <param name="list">附件路径</param>   
        /// <returns></returns>   
    
    public bool MySendMail(string mailto, string mailsubject, string mailbody, string mailFrom, ArrayList list)
    {
        try
        {
            //邮件发送人地址   
            System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(mailFrom);
            //如test@163.com,初步测试，用test@sina.com不行，用163的邮件服务器，就必须用163邮箱的用户名   
            //收件人地址   
            System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress(mailto);//如test@tom.com   
            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(from, to);
            mail.Subject = mailsubject;
            mail.Body = mailbody;
            //以下设置服务器   
            System.Net.Mail.SmtpClient mySmth = new System.Net.Mail.SmtpClient();
            //以下为增加附件   
            int count = list.Count;
            for (int i = 0; i < count; i++)
            {
                System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(list[i].ToString());
                mail.Attachments.Add(data);
            }
            mySmth.Send(mail);
            mail.Dispose();
            return true;
        }
        catch
        {
            return false;
        }
    }   
    public void AutoMail()
    {
       //自动发送邮件
        string mailSubject = "优秀资料确认！";
        string mailBody = "正文内容。";
        string mailFrom = ConfigurationManager.AppSettings["SendMail"];
        ArrayList List = new ArrayList();

        List.Add(Server.MapPath(ConfigurationManager.AppSettings["SendMailText"]));
        
        if (MySendMail(this.txtEmail.Text,mailSubject,mailBody, mailFrom, List))
        {
          //ShowAndRedirect(this, "注册确认函已经发送到你的邮箱，请查收!", "SendMail.aspx");
            string strScript = "<script language=javascript>alert('资料已经发送到您的邮箱！请确认！');</script>";
            this.Page.RegisterStartupScript("ShowTipMsg", strScript);
        }
        else
        {
          // ShowAndRedirect(this, "邮件发送失败!", "SendMail.aspx");
            string strScript = "<script language=javascript>alert('邮件发送失败！');</script>";
            this.Page.RegisterStartupScript("ShowTipMsg", strScript);
            return;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        AutoMail();

    }
}
